package com.example.DataSource.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.DataSource.Mapper.psychoMapper;
import com.example.DataSource.entity.answer_replies;
import com.example.DataSource.entity.member;
import com.example.DataSource.entity.psycho_ask;
import com.example.DataSource.serviceImpl.answerServiceImpl;
import com.example.DataSource.serviceImpl.memberServiceImpl;
import com.example.DataSource.serviceImpl.psychoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/api/ljkDemo/ask")
public class psychoController {
    @Autowired
    private psychoMapper psychoMapper;
    @Autowired
    private psychoServiceImpl psychoService;

    // 定义一个 GET 请求的接口，用于获取分页数据
    @GetMapping("/getall")
    public IPage<psycho_ask> getAll(int current, int size) {
        // 创建一个分页对象，指定当前页码和每页大小
        Page<psycho_ask> page = new Page<>(current, size);
        // 调用 Service 层的方法获取分页数据
        return psychoService.getPsychoAskPage(page);
    }

    @GetMapping("/deleteInfo")
    public void deleted(String id) {
        System.out.println("删除的是" + id);
        psychoService.removeById(id);
    }

    @GetMapping("/search")
    public IPage<psycho_ask> search(int current, int size, String searchInfo) {
        // 创建一个分页对象，指定当前页码和每页大小
        Page<psycho_ask> page = new Page<>(current, size);
        // 调用 Service 层的方法获取分页数据
        return psychoService.getPsychoAskPageSearch(page, searchInfo);
    }

    @Autowired
    private answerServiceImpl answerService;
    @PostMapping("/addAnswer")
    @Transactional
    public String answerQuestion(
            @RequestParam("questionId") int id,
            @RequestParam("content") String content,
            @RequestParam("memberId") int memberId,
            @RequestParam("parentId") int parent_id,
            @RequestParam("consultantId") int consultantId
    ) {
        answer_replies answer = new answer_replies();
        answer.setContent(content);
        answer.setMember_id(memberId);
        LocalDateTime now = LocalDateTime.now();
        String formattedDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now);
        answer.setReply_time(formattedDateTime);
        answer.setQuest_id(id);
        answer.setParent_id(parent_id);
        answer.setConsultant_id(consultantId);
        answerService.save(answer);
        return "回复成功";
    }
}
